package com.example.housekeeping.service.impl;

import com.example.housekeeping.ex.ServiceException;
import com.example.housekeeping.pojo.entity.Worker;
import com.example.housekeeping.pojo.dto.WorkerAddNewDTO;
import com.example.housekeeping.pojo.dto.WorkerUpdateDTO;
import com.example.housekeeping.pojo.vo.WorkerListItemVO;
import com.example.housekeeping.pojo.vo.WorkerStandardVO;
import com.example.housekeeping.mapper.WorkerMapper;
import com.example.housekeeping.pojo.vo.WorkerVo;
import com.example.housekeeping.service.WorkerService;
import com.example.housekeeping.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 家政服务员(Worker)表服务实现类
 *
 * @author bob
 * @since 2023-03-20 19:36:08
 */
@Slf4j
@Service
public class WorkerServiceImpl implements WorkerService {
    @Autowired
    private WorkerMapper workerMapper;

    /**
     * 新增数据
     *
     * @param workerAddNewDTO 实例对象
     * @return 实例对象
     */
    @Override
    public void addNew(WorkerAddNewDTO workerAddNewDTO) {
        log.debug("开始处理服务员添加工作,参数:{}",workerAddNewDTO);

        //检查用户名是否存在
        {
            Worker worker = new Worker();
            worker.setName(workerAddNewDTO.getName());
            long count = workerMapper.count(worker);
            if (count > 0){
                String message = "该用户名已存在!";
                log.warn(message);
                throw new ServiceException(ServiceCode.ERROR_CONFLICT,message);
            }
        }

        //检查手机是否被使用
        {
            Worker worker = new Worker();
            worker.setPhone(workerAddNewDTO.getPhone());
            long count = workerMapper.count(worker);
            if (count > 0){
                String message = "该手机已被使用!";
                log.warn(message);
                throw new ServiceException(ServiceCode.ERROR_CONFLICT,message);
            }
        }
        //创建worker对象
        Worker worker = new Worker();
        //赋值属性
        BeanUtils.copyProperties(workerAddNewDTO,worker);
        //密码加密

        //调用mapper对象insert()插入
        int rows = workerMapper.insert(worker);
        if (rows != 1){
            String message = "添加服务员失败,服务器忙,请稍后重试!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT,message);
        }
    }


    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public void deleteById(Long id) {
        log.debug("开始处理【根据id删除服务员】业务，参数：{}",id);
        Worker worker = new Worker();
        worker.setId(id);
        long count = workerMapper.count(worker);
        if (count != 1){
            String message = "当前id服务员不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        int rows = workerMapper.deleteById(id);
        if (rows != 1){
            String message = "删除服务员失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }
    }

    /**
     * 修改数据
     *
     * @param workerUpdateDTO 实例对象
     * @return 实例对象
     */
    @Override
    public void updateById(Long id, WorkerUpdateDTO workerUpdateDTO) {
        log.debug("开始处理【根据id修改服务员】业务，id参数：{},数据:{}",id,workerUpdateDTO);
        WorkerStandardVO queryResult = workerMapper.getStandardById(id);
        if (queryResult == null){
            String message = "当前id服务员不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }

        //检查用户名是否存在
        {
            Worker worker = new Worker();
            worker.setName(workerUpdateDTO.getName());
            long count = workerMapper.count(worker);
            if (count > 0){
                String message = "该用户名已存在!";
                log.warn(message);
                throw new ServiceException(ServiceCode.ERROR_CONFLICT,message);
            }
        }

        //检查手机是否被使用
        {
            Worker worker = new Worker();
            worker.setPhone(workerUpdateDTO.getPhone());
            long count = workerMapper.count(worker);
            if (count > 0){
                String message = "该手机已被使用!";
                log.warn(message);
                throw new ServiceException(ServiceCode.ERROR_CONFLICT,message);
            }
        }
        //创建服务员对象
        Worker worker = new Worker();
        //赋值属性
        BeanUtils.copyProperties(workerUpdateDTO,worker);
        //设置id
        worker.setId(id);
        //调用update方法
        workerMapper.update(worker);
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public WorkerStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据ID查询服务员详情】的业务，参数：{}", id);
        WorkerStandardVO queryResult = workerMapper.getStandardById(id);
        if (queryResult == null){
            // 是：抛出异常
            String message = "查询服务员详情失败，该服务员数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        return queryResult;
    }

    /**
     * 查询列表数据
     *
     * @return 实例对象
     */
    @Override
    public List<WorkerListItemVO> list() {
        log.debug("开始处理【查询服务员列表】的业务，无参数");
         List<WorkerListItemVO> list = workerMapper.list();
        return list;
    }

    @Override
    public WorkerVo login(WorkerAddNewDTO workerAddNewDTO, HttpSession session) {
        WorkerVo w = workerMapper.getVoByUsername(workerAddNewDTO.getName());
        System.out.println(w.getPassword());
        System.out.println(workerAddNewDTO.getPassword());
        if (w != null){
            if (w.getPassword().equals(workerAddNewDTO.getPassword())){
                session.setAttribute("worker",w);
                return w;
            }
            String message = "密码错误";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT,message);
        }
        String message = "当前用户不存在";
        log.warn(message);
        throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
    }

    @Override
    public void register(WorkerAddNewDTO workerAddNewDTO) {
        WorkerVo w = workerMapper.getVoByUsername(workerAddNewDTO.getName());
        if (w != null){
            String message = "当前用户已存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT,message);
        }
        workerMapper.addWorker(workerAddNewDTO);
    }

}
